<template>
    <div>
        <el-card>
            <!-- table展示区 -->
            <el-table border :data="records">
                <el-table-column  label="序号" width="80px" style="index" align="center"></el-table-column>
                <el-table-column  prop="skuName" label="名称" show-overflow-tooltip width="150px" ></el-table-column>
                <el-table-column  prop="skuDesc" label="描述" show-overflow-tooltip width="150px"></el-table-column>
                <el-table-column  label="图片" width="150px">
                    <template #="{row}" >
                        <img :src="row.skuDefaultImg" alt="" style="width: 100px; height:100px;">
                    </template>
                </el-table-column>
                <el-table-column  prop="weight" label="重量(g)" width="150px"></el-table-column>
                <el-table-column  prop="price" label="价格(元)" width="150px"></el-table-column>
                <el-table-column  label="操作" width="250px" fixed="right">
                    <template #="{row}">
                        <el-button @click="updateSale(row)" type="primary" :icon="row.isSale==0?'Top':'Bottom'" size="small"></el-button>
                        <el-button @click="editSale" type="primary" icon="Edit" size="small"></el-button>
                        <el-button @click="drawerClick(row)" type="primary" icon="InfoFilled" size="small"></el-button>
                        <el-popconfirm :title="`你确定要删除${row.skuName}吗?`" @confirm="removeSku(row)">
                            <template #reference>
                                <el-button type="primary" icon="Delete" size="small"></el-button>
                            </template>
                        </el-popconfirm>
                        
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页器 -->
            <el-pagination
                v-model:current-page="pageNow"
                v-model:page-size="pageSize"
                :page-sizes="[10, 20, 30, 40]"
                :background="true"
                layout=" prev, pager, next, jumper,->,sizes,total"
                :total="total"
                @current-change="getSku" 
                @size-change="changeSize"
            />
            <!-- 抽屉组件,展示商品详情 -->
            <el-drawer v-model="drawer">
                <template #header>
                    <span>商品详情</span>
                </template>
                <template #default>
                    <el-row style="margin: 10px 0;">
                        <el-col :span="6">名称</el-col>
                        <el-col :span="18">{{ skuInfo.skuName }}</el-col>
                    </el-row >
                    <el-row style="margin: 10px 0;">
                        <el-col :span="6">描述</el-col>
                        <el-col :span="18">{{ skuInfo.skuDesc }}</el-col>
                    </el-row>
                    <el-row style="margin: 10px 0;">
                        <el-col :span="6">价格</el-col>
                        <el-col :span="18">{{ skuInfo.price }} 元</el-col>
                    </el-row>
                    <el-row style="margin: 10px 0;">
                        <el-col :span="6">平台属性</el-col>
                        <el-col :span="18">
                            <el-tag v-for="item in skuInfo.skuAttrValueInfoList" :key="item._id" style="margin: 5px;">{{ item.name }}</el-tag>
                        </el-col>
                    </el-row>
                    <el-row style="margin: 10px 0;">
                        <el-col :span="6">商品属性</el-col>
                        <el-col :span="18">
                            <el-tag v-for="item in skuInfo.skuSaleAttrValueInfoList" :key="item.id" style="margin: 5px;">{{item.saleAttrValueName}}</el-tag>
                        </el-col>
                    </el-row>
                    <el-row style="margin: 10px 0;">
                        <el-col :span="6">图片</el-col>
                        <el-col :span="18">
                            <el-carousel :interval="2000" type="card" height="200px">
                                <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
                                    <img :src="item.imgUrl" alt="" style="width: 120%;height: 100%;">
                                </el-carousel-item>
                            </el-carousel>
                        </el-col>
                    </el-row>
                </template>
            </el-drawer>
        </el-card>

    </div>
</template>

<script setup lang='ts'>
import { onMounted, ref } from 'vue';
import { reqSkuData,reqSkuOnSale,reqSkuDownSale ,reqGetSkuInfo,reqDeleteSkuInfo} from '@/api/product/sku';
import type {skuResposeData,Records} from '@/api/product/sku/type'
import { ElMessage } from 'element-plus';

// 分页器当前页码
const pageNow=ref<number>(1)
//一页多少个
const pageSize=ref<number>(10)
//总数
const total=ref<number>()
//sku
const records=ref<Records[]>([])
//控制详情页显隐
const drawer=ref<boolean>(false)
//存储sku详情信息
const skuInfo=ref<Records>({})
//挂载先发送一次请求
onMounted(()=>{
    getSku()
})
const getSku=async(pager=1)=>{
    //收集当前页码
    pageNow.value=pager
    //发送请求和获取sku数据
    let result:skuResposeData=await reqSkuData(pageNow.value,pageSize.value)
    if(result.code==200){
        //存储数据展示
        total.value=result.data.total
        records.value=result.data.records
    }

    
}
//分页器下拉菜单改变回调
const changeSize=()=>{
    getSku()
}
//商品上下架更新
const updateSale=async(row:Records)=>{
    console.log(row.id);
    if(row.isSale==0){
        let result:any=await reqSkuOnSale((row.id as number))
        if(result.code==200){
            ElMessage({
                type:'success',
                message:'上架成功'
            })
            getSku()
        }else{
            ElMessage({
                type:'error',
                message:'上架失败'
            })
        }
    }else{
        let result=await reqSkuDownSale((row.id as number))
        if(result.code==200){
            ElMessage({
                type:'success',
                message:'下架成功'
            })
            getSku()
        }else{
            ElMessage({
                type:'error',
                message:'下架失败'
            })
        }
    }
}
//sku编辑按钮回调
const editSale=()=>{
    ElMessage({
        type:'success',
        message:'暂无此模块~~~'
    })
}
//商品详情点击按钮
const drawerClick=async (row:Records)=>{
    let result=await reqGetSkuInfo((row.id as number))
    skuInfo.value=result.data
    //显示走马灯详细商品信息
    drawer.value=true 
}
//删除sku
const removeSku=async (row:Records)=>{
    
    let result=await reqDeleteSkuInfo((row.id as number))
    if(result.code==200){
        ElMessage({
            type:'success',
            message:'删除成功'
        })
        getSku(records.value?.length>1?pageNow.value:pageNow.value-1)
    }else{
        ElMessage({
            type:'error',
            message:'删除失败'
        })
    }
}

</script>

<style scoped lang='scss'>
.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>